<?php
	function addHeader($name)
	{
		$insert  = "INSERT INTO ".DB_PERFIX."com_headers (`id`, `name`)"; 
		$insert .= "VALUES (NULL, '".$_POST['name']."');";
		$result = mysql_query($insert);
		if ($result > 0)
		{
			$result = mysql_query("SELECT id FROM ".DB_PERFIX."com_headers WHERE name LIKE '".$_POST['name']."';");
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['title'] = $name;
			}
		}
		return $item;
	}
	
	function getHeaderAdmin($id)
	{
		$result = mysql_query("SELECT * FROM ".DB_PERFIX."com_headers WHERE id= ".$id.";");
		while($row = mysql_fetch_array($result))
		{
			$header['id'] = $row['id'];
			$header['name'] = $row['name'];
			$header['l_image'] = $row['l_image'];
			$header['image'] = $row['image'];
			$header['image2'] = $row['image2'];
			$header['r_image'] = $row['r_image'];
			$header['height'] = $row['height'];
			$header['button'] = $row['button'];
			$header['icon'] = $row['icon'];
			$header['imagefont'] = $row['imagefont'];
			$header['active'] = $row['active'];
			$header['css'] = getCssArray(1, $row['id']);
		}
		return $header;
	}
	
	function getHeaders($incat=0)
	{
		$i=0;
		if($incat > 0)
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."com_headers WHERE id IN(SELECT item_id FROM ".DB_PERFIX."admin_hfb_cats_to_items WHERE type=1 AND cat_id=".$incat.");"); }
		else
		{ $result = mysql_query("SELECT * FROM ".DB_PERFIX."com_headers;"); }
		while($row = mysql_fetch_array($result))
		{
			$header[$i]['id'] = $row['id'];
			$header[$i]['name'] = $row['name'];
			$header[$i]['l_image'] = $row['l_image'];
			$header[$i]['image'] = $row['image'];
			$header[$i]['image2'] = $row['image2'];
			$header[$i]['r_image'] = $row['r_image'];
			$header[$i]['height'] = $row['height'];
			$header[$i]['button'] = $row['button'];
			$header[$i]['icon'] = $row['icon'];
			$header[$i]['imagefont'] = $row['imagefont'];
			$header[$i]['active'] = $row['active'];
			$i++;
		}
		return $header;
	}
	function updateHeader($id, $name, $height, $imagefont, $button=0, $icon=0)
	{
		$update  = 'UPDATE '.DB_PERFIX.'com_headers SET ';
		$update .= 'name="'.$name.'", ';
		$update .= 'height='.$height.', ';
		$update .= 'button='.$button.', ';
		$update .= 'icon='.$icon.', ';
		$update .= 'imagefont='.$imagefont.' ';
		$update .= 'WHERE id='.$id.' LIMIT 1;';
		$update = mysql_query($update);
	}
	function deleteHeader($id)
	{
		if($id > 0)
		{
			$item = getHeaderAdmin($id);
			if($item)
			{
				if($item['l_image'] != '')
				{ deleteHeaderImage($id, 'l_image', $item['l_image']); }
				if($item['r_image'] != '')
				{ deleteHeaderImage($id, 'r_image', $item['r_image']); }
				if($item['image'] != '')
				{ deleteHeaderImage($id, 'image', $item['image']); }
				if($item['image2'] != '')
				{ deleteHeaderImage($id, 'image2', $item['image2']); }
				$result = mysql_query('DELETE FROM '.DB_PERFIX.'com_headers WHERE id='.$id.';');
				return $result;
			}
		}
	}
	function deleteHeaderImage($id, $field, $filename)
	{
		if (!unlink(HEADERS_PATH.$filename))
		{ $status = "Error deleting ".HEADERS_PATH.$filename; }
		else
		{ 
			$result = mysql_query("UPDATE ".DB_PERFIX."com_headers SET ".$field." = '' WHERE id=".$id.";");
			if ($result > 0)
			{$status = "Left Image Deleted";}
		}
		return $status;
	}
	function uploadHeaderImages($id)
	{
		if (image_valid($_FILES['image']['type']) && ($_FILES["file"]["size"] < 20000) && ($_FILES['image']['name'] != ""))
		{
			$temp_name = $_FILES["image"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$id."_bg.".$extension;
			
			if (file_exists(HEADERS_PATH.$new_name))
			{$error = $_FILES["image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image"]["tmp_name"], HEADERS_PATH.$new_name);
				$error = "Stored in: " . HEADERS_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."com_headers SET image = '".$new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$ImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['image2']['type']) && ($_FILES["image2"]["size"] < 20000) && ($_FILES['image2']['name'] != ""))
		{
			$temp_name = $_FILES["image2"]["name"];
			$extension = substr($temp_name,-3);
			$new_name = "type_".$id."_bg2.".$extension;
			
			if (file_exists(HEADERS_PATH.$new_name))
			{$error = $_FILES["image2"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["image2"]["tmp_name"], HEADERS_PATH.$new_name);
				$error = "Stored in: " . HEADERS_PATH.$new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."com_headers SET image2 = '".$new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$ImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";}
			}
			$extension = "";
		}
		if (image_valid($_FILES['l_image']['type']) && ($_FILES["l_image"]["size"] < 20000) && ($_FILES['l_image']['name'] != ""))
		{
			$l_temp_name = $_FILES["l_image"]["name"];
			$l_extension = substr($l_temp_name,-3);
			$l_new_name = "type_".$id."_l.".$l_extension;
			
			if (file_exists(HEADERS_PATH.$l_new_name))
			{$error = $_FILES["l_image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["l_image"]["tmp_name"], HEADERS_PATH.$l_new_name);
				$error = "Stored in: " . HEADERS_PATH.$l_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."com_headers SET l_image = '".$l_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$lImageStatus = "<strong>Image Uploaded ".$l_new_name."</strong>";}
			}
		}
		if (image_valid($_FILES['r_image']['type']) && ($_FILES["r_image"]["size"] < 20000) && ($_FILES['r_image']['name'] != ""))
		{
			$r_temp_name = $_FILES["r_image"]["name"];
			$r_extension = substr($r_temp_name,-3);
			$r_new_name = "type_".$id."_r.".$r_extension;
			
			if (file_exists(HEADERS_PATH.$r_new_name))
			{$error = $_FILES["r_image"]["name"] . " already exists. ";}
			else
			{
				move_uploaded_file($_FILES["r_image"]["tmp_name"], HEADERS_PATH.$r_new_name);
				$error = "Stored in: " . HEADERS_PATH.$r_new_name;
				
				$result = mysql_query("UPDATE ".DB_PERFIX."com_headers SET r_image = '".$r_new_name."' WHERE id = '".$id."'");
				if ($result > 0)
				{$rImageStatus = "<strong>Image Uploaded ".$r_new_name."</strong>";}
			}
		}
	}
?>